<?php
  define('ACC', true);
require('./include/init.php');

session_start(); 
//print_r($_SESSION);
$userid = $_SESSION['user_id'];

//echo '<head><meta charset = "utf-8"></head>';  
//$userid = 26;
//echo '<h2>采购&nbsp&nbsp<a href = "dashboard.php">返回虚拟公司主页</a></h2><br/>';   

$company = new companymodel();
$companyinfo = $company->getcompanyinfo($userid);
$companyid = $companyinfo['company_id'];

$applyforloaninfo = $_POST;
$applyforloanmsg='';

foreach($applyforloaninfo as $k=>$v){
    if(($v!='')&&($v!='0')){
        $elementname = $company->getelementname($k);
        $succeed = $company->actionapplyforloan($companyid, $k, $v);
        if($succeed === false){
            $applyforloanmsg = $applyforloanmsg . '申请'.$elementname.'：'. $v .'->失败！<br/>';        
        }else{
            $applyforloanmsg = $applyforloanmsg . '申请'.$elementname.'：'. $v .'->成功！<br/>';
        }
    }
}

    $cashequivalent = $company->getcash_equivalent($companyid);
    $loan =  $company->getloan($companyid);
    $totalcashequivalent = 0;
    $totalloan = 0;
    foreach($cashequivalent  as $k=>$v){
        $totalcashequivalent = $totalcashequivalent + $v;
    }

    foreach($loan  as $k=>$v){
        $totalloan = $totalloan +$v['current_value'];
    }



    echo "
    { \"applyforloanmsg\" : \"{$applyforloanmsg}\" , 
      \"totalcashequivalent\" : \"{$totalcashequivalent}\",
      \"totalloan\" : \"{$totalloan}\"";
      

      
    echo '
    }
    ';






?>
